package com.example.administrator.text;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import app.BaseActivity;
import app.MyApplication;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import data.SharedPreferencesUtils;
import util.BitmapUtil;
import util.HomeUtil;
import util.JumpActivityUtils;
import util.LogUtil;
import util.Url;
import view.WeLinearLayout;
import view.diaLogView.RetunDialog;
import view.diaLogView.WeAccountShowImageDialog;

/**
 * 修改个人资料的页面
 */
public class  WeInformationActivity extends BaseActivity {
    private RetunDialog mRetunDialog;
    private WeAccountShowImageDialog mWeAccountShowImageDialog;
    private String IMAGE_FILE_NAME = "clip_temp.jpg";//选择头像 相机 图片保存的名字
    private String RESULT_PATH = "result_path";
    private String PASS_PATH = "pass_path";
    private  final int CUT = 3;
    private int mPwd;
    private boolean mState = false;  //记录退出是否被点击过

    @Bind(R.id.frame_weinformation)       FrameLayout mFrameLayout;
    @Bind(R.id.image_weinformtihead_head) ImageView mImageView;
    @Bind(R.id.welinear_accountnumber)    WeLinearLayout mWeLayoutAccountnumber;      //账号
    @Bind(R.id.welinear_autograph)        WeLinearLayout mWeLayoutAutograph;         //签名
    @Bind(R.id.welinear_gender)           WeLinearLayout mWeLayoutGender;           //性别
    @Bind(R.id.welinear_nickname)         WeLinearLayout mWeLayoutNickname;        // 昵称
    @Bind(R.id.welinear_password)         WeLinearLayout mWeLayoutPassword;       //密码
    @Bind(R.id.welinear_phone)            WeLinearLayout mWeLayoutPhone;         //手机

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weinformation);
        ButterKnife.bind(this);
        init();

    }

    private void init() {
        HomeUtil.getHemeUtilNew().getStatusBar(this, mFrameLayout);
        setInformation();
        mWeLayoutAccountnumber.setOnWeLinearClickListener(new WeLinearLayout.OnWeLinearItemClickListener() {
          @Override
          public void onItemClick() {
             // JumpActivityUtils.jumpModifypassword(WeInformationActivity.this,ModifyInformationActivity.SELFSOM);
          }
        });

        mWeLayoutAutograph.setOnWeLinearClickListener(new WeLinearLayout.OnWeLinearItemClickListener() {
            @Override
            public void onItemClick() {
                JumpActivityUtils.getInstance().jumpModifypassword(WeInformationActivity.this,ModifyInformationActivity.SELFSOM);
            }
        });

        mWeLayoutGender.setOnWeLinearClickListener(new WeLinearLayout.OnWeLinearItemClickListener() {
            @Override
            public void onItemClick() {
                JumpActivityUtils.getInstance().jumpModifypassword(WeInformationActivity.this,ModifyInformationActivity.SEX);
            }
        });

        mWeLayoutNickname.setOnWeLinearClickListener(new WeLinearLayout.OnWeLinearItemClickListener() {
            @Override
            public void onItemClick() {
                JumpActivityUtils.getInstance().jumpModifypassword(WeInformationActivity.this,ModifyInformationActivity.NICKNAME);
            }
        });

        mWeLayoutPassword.setOnWeLinearClickListener(new WeLinearLayout.OnWeLinearItemClickListener() {
            @Override
            public void onItemClick() {
                JumpActivityUtils.getInstance().jumpModifypassword(WeInformationActivity.this,ModifyInformationActivity.PWD);
            }
        });

        mWeLayoutPhone.setOnWeLinearClickListener(new WeLinearLayout.OnWeLinearItemClickListener() {
            @Override
            public void onItemClick() {
                JumpActivityUtils.getInstance().jumpModifypassword(WeInformationActivity.this,ModifyInformationActivity.PHONE);
            }
        });
    }

    /**
     * 返回
     */
    @OnClick(R.id.weinformation_image_about)
    void weinformation(){
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
        LogUtil.eE("","weinformation");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        intent.putExtra("type","");
        finish();
        return false;
    }

    /**
     * 头像
     */
    @OnClick(R.id.linear_weinformtihead_head)
    void weinformtihead_head(){
        mWeAccountShowImageDialog = new WeAccountShowImageDialog(this,"",mBaseImageLoader,null);
        mWeAccountShowImageDialog.show();
    }

    /**
     * 退出
     * 如果没有设置密码就去提示设置密码
     */
    @OnClick(R.id.linear_weinformation_out)
    void weinformationOut(){

        mPwd = (int)SharedPreferencesUtils.getParam(this,ModifyInformationActivity.PWD,1);  //密码
        if(mPwd == 1){

           String fwq = (String) SharedPreferencesUtils.getParam(this,HomeUtil.mFWQ,"");
           if(fwq.equals("")){
             fwq = MyApplication.getApp().getFWQString();
           }
            if(mRetunDialog == null){
                mRetunDialog = new RetunDialog(this,fwq,0,false);
                mRetunDialog.show();
            }else {
                mRetunDialog.show();
            }

        }else {
            if(!mState){
                mState = true;
                getToast("您还未设置密码请先设置密码");
                JumpActivityUtils.getInstance().jumpModifypassword(WeInformationActivity.this,ModifyInformationActivity.PWD);
            }else {
                String fwq = (String) SharedPreferencesUtils.getParam(this,HomeUtil.mFWQ,"");
                if(fwq.equals("")){
                    fwq = MyApplication.getApp().getFWQString();
                }

                if(mRetunDialog == null){
                    mRetunDialog = new RetunDialog(this,fwq,0,false);
                    mRetunDialog.show();
                }else {
                    mRetunDialog.show();
                }
            }
        }
    }

    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode ){
            case 0:
                String type = data.getStringExtra("type");
                String content = data.getStringExtra("content");
                if(type.equals(ModifyInformationActivity.PWD)){
                    mWeLayoutPassword.setTextViewlefg("******");
                    mWeLayoutPassword.setRoundIVState(false);
                }else if(type.equals(ModifyInformationActivity.NICKNAME)){
                    mWeLayoutNickname.setTextViewlefg(content);
                }else if(type.equals(ModifyInformationActivity.SEX)){
                    mWeLayoutGender.setTextViewlefg(content);
                }else if(type.equals(ModifyInformationActivity.SELFSOM)){
                    mWeLayoutAutograph.setTextViewlefg(content);
                }else if(type.equals(ModifyInformationActivity.PHONE)){
                    mWeLayoutPhone.setTextViewlefg(content);
                }
                break;
            case 1:
                Log.e("pat","头像相册");
                startCropImageActivity(getFilePath(data.getData()));
                break;
            case 2:
                Log.e("pat", "头像相册");
                // 照相机程序返回的,再次调用图片剪辑程序去修剪图片
                startCropImageActivity(Environment.getExternalStorageDirectory() + "/" + IMAGE_FILE_NAME);
                break;
            case 3:
                String path = data.getStringExtra(RESULT_PATH);
                LogUtil.eE("","WeAccountShowImageActivity       "+path);
                Bitmap bitmap = BitmapFactory.decodeFile(path);
                Url.getUrlNew().setWE_PORTRAIT(path);
                mImageView.setImageBitmap(bitmap);
                break;
        }

    }

    /**
     * 设置相关信息
     */
    private void setInformation(){
        String headUrl = (String) SharedPreferencesUtils.getParam(this,ModifyInformationActivity.WEHEAD,"");     //头像地址
        String selfsom = (String)SharedPreferencesUtils.getParam(this,ModifyInformationActivity.SELFSOM,"");    //个性
        int sex = (int)SharedPreferencesUtils.getParam(this,ModifyInformationActivity.SEX,1);                  //性别
        String nicKname = (String)SharedPreferencesUtils.getParam(this,ModifyInformationActivity.NICKNAME,"");//昵称
        int niunum = (int)SharedPreferencesUtils.getParam(this,ModifyInformationActivity.NIUNUM,1);       //牛号
        mPwd = (int)SharedPreferencesUtils.getParam(this,ModifyInformationActivity.PWD,1);               //密码
        String phone = (String)SharedPreferencesUtils.getParam(this,HomeUtil.mHUWePhone,"");            //手机


        mWeLayoutAccountnumber.setTextViewlefg(niunum+"");
        mWeLayoutPhone.setTextViewlefg(phone);

        if(selfsom.equals("")){
            mWeLayoutAutograph.setTextViewlefg("未录入");
        }else {
            mWeLayoutAutograph.setTextViewlefg(selfsom);
        }

        if(nicKname.equals("")){
            mWeLayoutNickname.setTextViewlefg("未录入");
        }else {
            mWeLayoutNickname.setTextViewlefg(nicKname);
        }

        if(mPwd == 1){
            mWeLayoutPassword.setTextViewlefg("******");
            mWeLayoutPassword.setRoundIVState(false);
        }else if(mPwd == 0){
            mWeLayoutPassword.setRoundIVState(true);
            mWeLayoutPassword.setTextViewlefg("未录入");
        }else {
            mWeLayoutPassword.setRoundIVState(true);
            mWeLayoutPassword.setTextViewlefg("未录入");
        }

        if(sex == 1){
            LogUtil.eE("",sex+"");
            mWeLayoutGender.setTextViewlefg("男");
        }else if(sex == 2){
            mWeLayoutGender.setTextViewlefg("女");
        }else {
            mWeLayoutGender.setTextViewlefg("未录入");
        }

        if(Url.getUrlNew().getWE_PORTRAIT().equals("")){
            BitmapUtil.newBitmap().showBitmap(this,headUrl,mImageView,R.mipmap.weaccount_head,null);
        }else {
            Bitmap bitmap = BitmapFactory.decodeFile(Url.getUrlNew().getWE_PORTRAIT());
            mImageView.setImageBitmap(bitmap);
        }

    }

    /**
     *裁剪图片的Activity
     */
    private void startCropImageActivity(String path) {
        File file = new File(path);
        LogUtil.eE("path",""+path);
        LogUtil.eE("path",""+file.length());
        if(file.length() !=0){
            Intent intent = new Intent(this, ClipImageActivity.class);
            intent.putExtra(PASS_PATH, path);
            startActivityForResult(intent,CUT);
        }else {getToast("请重新选择图片");}

    }

    /**
     * 通过uri获取文件路径
     *
     * @param mUri
     */
    public String getFilePath(Uri mUri) {
        try {
            if (mUri.getScheme().equals("file")) {
                return mUri.getPath();
            } else {
                return getFilePathByUri(mUri);
            }
        } catch (FileNotFoundException ex) {
            return null;
        }
    }

    // 获取文件路径通过url
    private String getFilePathByUri(Uri mUri) throws FileNotFoundException {
        Cursor cursor = getContentResolver()
                .query(mUri, null, null, null, null);
        cursor.moveToFirst();
        return cursor.getString(1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mRetunDialog !=null){
            mRetunDialog.dismiss();
        }
        if(mWeAccountShowImageDialog !=null){
            mWeAccountShowImageDialog.dismiss();
        }
    }
}
